package savelog;

import java.util.ArrayList;
import java.util.Hashtable;

import android.app.Activity;
import android.app.Fragment;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TableRow.LayoutParams;

/**
 * Title: GameListFragment class for CSIS 343 Final Project 
 * Description: CS 343 Final Project - GameListFragment This class will handle displaying the list of games in the GameListActivity
 * 
 * @author Erik Olson
 * @email erik.olson-5@my.uwrf.edu
 * @author Mike Harmon
 * @email michael.harmon@my.uwrf.edu
 * @author Jack Friermuth
 * @email jack.friermuth@my.uwrf.edu
 * @author Luke Vang
 * @email luke.vang@my.uwrf.edu
 * @author Casey Golla
 * @email casey.golla@my.uwrf.edu
 * @date December 4th 2012
 */
public class GameListFragment extends Fragment{
	private Button sortAZ;
	private Button sortLatestGame;
	private Button sysDisplayed;
	
	private TableLayout gameTable;
	private Boolean isUpdateAct;
	private Hashtable<Integer, Boolean> selectedGames;
	
	private ArrayList<Games> gameArrayList;
	
	private View currSelectedGame = null;
	private int currSelectedGameID;
	
	private GameListListener listener;
	
	public interface GameListListener{
		public void onGameSelected(int selectedGameID);
		public void onSystemSelected();
		public void onSortAZ();
		public void onSortLatest();
	}
	
	/**
	 * method to call the parents create
	 * @Override
	 */
	public void onCreate( Bundle savedInstanceState ){
		super.onCreate( savedInstanceState );
	}
	
	/**
	 * Method that creates the view of the fragment
	 * @param LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState
	 * @Override
	 * @return view
	 */
	public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ){
		View view = inflater.inflate( R.layout.fragment_gamelist_layout, container, false );
		
		//creating hash table to hold selected games
		selectedGames = new Hashtable<Integer, Boolean>();
		
		//setting up buttons and click listener
		sortAZ = (Button) view.findViewById( R.id.buttonSortAlpha );
		sortAZ.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				listener.onSortAZ();
			}
		});
		
		sortLatestGame = (Button) view.findViewById( R.id.buttonSortLatest );
		sortLatestGame.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				listener.onSortLatest();
			}
		});
		
		sysDisplayed = (Button) view.findViewById( R.id.buttonSelectSystem );
		
		sysDisplayed.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				listener.onSystemSelected();
			}
		});
		
		gameTable = (TableLayout) view.findViewById( R.id.gameTable );
		
		//creates the table layout for games
		createGameTable();
		
		return view;
	}
	
	/**
	 * Method to fire when gameList is attached to activity
	 * @Override
	 */
    public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof GameListListener){
			listener = (GameListListener) activity;
		}
	}
	
    /**
     * Method setting the game list data
     * @param listData
     * @param updateAct
     */
	public void setGameListData( ArrayList<Games> listData, boolean updateAct ){
		gameArrayList = listData;
		isUpdateAct = updateAct;
	}
	/**
	 * Method to return a hashtable of selectedgames
	 * @return selectedGame
	 */
	public Hashtable<Integer, Boolean> getSelectedGamesHashtable(){
		return selectedGames;
	}
	
	/**
	 * Method to creates the table layout for games
	 */
	public void createGameTable(){
		//Our main table for each section(fTable) consists of multiple rows(fTableRow),
		//separated by a line(fRowLine)
		TableRow gameTableRow = null;
		TextView gameTitle;

		TableRow.LayoutParams gameRowParams = new TableRow.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT );
		int gameRowPaddingLR = dpToPixels(4);
		int gameRowPaddingUD = dpToPixels(8);
		
		//looping through all games adding them to the game list
		for( final Games game : gameArrayList ){
			
			gameTableRow = new TableRow( getActivity() );
			gameTableRow.setLayoutParams( gameRowParams );
			gameTableRow.setTag(game.getGame_id());
			
			gameTableRow.setClickable( true );
			gameTableRow.setOnClickListener( new OnClickListener() {

				@Override
				public void onClick( View v ) {
					if(isUpdateAct){
						Integer selectedGameID = (Integer) v.getTag();
						Boolean gameSelectedValue = (Boolean) v.isSelected();
						v.setSelected(!gameSelectedValue);
						selectedGames.put(selectedGameID, !gameSelectedValue);
					}
					else{
						if(currSelectedGame != null){
							currSelectedGame.setSelected(false);
						}
						v.setSelected(true);
						currSelectedGame = v;
						currSelectedGameID = (Integer) v.getTag();
						
						listener.onGameSelected(currSelectedGameID);
					}
				}
				
			});
			
			gameTableRow.setBackgroundResource( R.drawable.gamerow_selector );
			
			gameTitle = new TextView( getActivity() );
			gameTitle.setText( game.getGame_title() );
			gameTitle.setTextAppearance( getActivity(), R.style.GameListTitle );
			gameTitle.setPadding(gameRowPaddingLR, gameRowPaddingUD, gameRowPaddingLR, gameRowPaddingUD);
			
			gameTableRow.addView( gameTitle );
			
			gameTable.addView( gameTableRow );
		}
		
	}
	
	/**
	 * Method to convert dp to pixels
	 * @param dp
	 * @return px number of pixels
	 */
	public int dpToPixels(int dp) {
		Resources r = getActivity().getResources();
		int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				dp, r.getDisplayMetrics());
		return px;
	}
	
	/**
	 * Method to insert state information for later recall
	 * @Override
	 */
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curGame", currSelectedGameID);
    }
}
